package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.RestaurantHotelDAO;
import bist.believeinstep.domain.RestaurantHotel;
import bist.believeinstep.mapper.RestaurantHotelMapper;

public class RestaurantHotelTemplate implements RestaurantHotelDAO, ConnectionDAO{
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(RestaurantHotel restaurantHotel){
		String query = "INSERT INTO bist_restaurant_hotels ("
				+ "name, avatar, phone, email,address, address1, address2, state, star, descriptions, rate, type,"
				+ "created_date, modified_date, created_by, modified_by) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
				restaurantHotel.getName(),
				restaurantHotel.getAvatar(),
				restaurantHotel.getPhone(),
				restaurantHotel.getEmail(),
				restaurantHotel.getAddress(),
				restaurantHotel.getAddress1(),
				restaurantHotel.getAddress2(),
				restaurantHotel.getState(),
				restaurantHotel.getStar(),
				restaurantHotel.getDescription(),
				restaurantHotel.getRate(),
				restaurantHotel.getType(),
				restaurantHotel.getCreated(),
				restaurantHotel.getModified(),
				restaurantHotel.getCreatedBy(),
				restaurantHotel.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(RestaurantHotel restaurantHotel){
		String query = "UPDATE bist_restaurant_hotels SET"
				+ "name = ?"
				+ "avatar = ?"
				+ "phone = ?"
				+ "email = ?"
				+ "address = ?"
				+ "address1 = ?"
				+ "address2 = ?"
				+ "state = ?"
				+ "star = ?"
				+ "descriptions = ?"
				+ "rate = ?"
				+ "type = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
					restaurantHotel.getName(),
					restaurantHotel.getAvatar(),
					restaurantHotel.getPhone(),
					restaurantHotel.getEmail(),
					restaurantHotel.getAddress(),
					restaurantHotel.getAddress1(),
					restaurantHotel.getAddress2(),
					restaurantHotel.getState(),
					restaurantHotel.getStar(),
					restaurantHotel.getDescription(),
					restaurantHotel.getRate(),
					restaurantHotel.getType(),
					restaurantHotel.getCreated(),
					restaurantHotel.getModified(),
					restaurantHotel.getCreatedBy(),
					restaurantHotel.getModifiedBy()
				});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_restaurant_hotels WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(String name){
		String query = "DELETE FROM bist_restaurant_hotels WHERE name = ?";
		try{
			jdbcTemplate.update(query, new Object[]{name});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public RestaurantHotel getRestaurantHotel(Integer id){
		String query = "SELECT FROM bist_restaurant_hotels WHERE id = ?";
		try{
			RestaurantHotel restaurantHotel = jdbcTemplate.queryForObject(query, new Object[]{id},
					new RestaurantHotelMapper());
			return restaurantHotel;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public RestaurantHotel getRestaurantHotel(String name){
		String query = "SELECT FROM bist_restaurant_hotels WHERE name = ?";
		try{
			RestaurantHotel restaurantHotel = jdbcTemplate.queryForObject(query, new Object[]{name},
					new RestaurantHotelMapper());
			return restaurantHotel;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<RestaurantHotel> list(){
		String query = "SELECT * FROM bist_restaurant_hotels";
		try{
			List<RestaurantHotel>restaurantHotel = jdbcTemplate.query(query, new RestaurantHotelMapper());
			return restaurantHotel;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
